package com.yc.boot.train.business;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yc.boot.common.model.base.BasePageQuery;
import com.yc.boot.common.model.response.Outcome;
import com.yc.boot.sys.business.SysUserBusiness;
import com.yc.boot.sys.entity.SysUser;
import com.yc.boot.sys.entity.SysUserRole;
import com.yc.boot.sys.model.UserInfo;
import com.yc.boot.sys.model.find.FindUserPage;
import com.yc.boot.sys.service.SysUserRoleService;
import com.yc.boot.sys.service.SysUserService;
import com.yc.boot.train.entity.GradeCommentFeedback;
import com.yc.boot.train.entity.TrainCourse;
import com.yc.boot.train.model.find.FindUserByCourse;
import com.yc.boot.train.model.find.GradeManage;
import com.yc.boot.train.model.find.IPageCourse;
import com.yc.boot.train.model.find.IPageGradeManage;
import com.yc.boot.train.model.save.SaveGradeManage;
import com.yc.boot.train.model.update.UpdataGradeManage;
import com.yc.boot.train.service.GradeCommentFeedbackService;
import com.yc.boot.train.service.GradeManageService;
import com.yc.boot.train.service.TrainCourseService;
import com.yc.boot.train.service.UserCourseService;
import com.yc.boot.work.entity.TestClass;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import sun.invoke.empty.Empty;

import javax.annotation.Resource;
import javax.validation.constraints.Null;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import static com.yc.boot.common.constant.BaseConstant.ZERO;
import static com.yc.boot.common.constant.RedisConstant.SYS_USER_CACHE;

/**
 * Create with IntelliT IDEA
 *
 * @Author: zhengmingzhe
 * @Date: 2021/12/30/16:26
 * @Description:
 */
@Slf4j
@Service
public class GradeManageBussiness {
    @Resource
    private GradeManageService gradeManageService;

    @Resource
    private SysUserBusiness sysUserBusiness;

    @Resource
    private UserCourseService userCourseService;

    @Resource
    private TrainCourseService trainCourseService;

    @Resource
    private SysUserService sysUserService;

    @Resource
    private SysUserRoleService sysUserRoleService;

    @Resource
    private GradeCommentFeedbackService gradeCommentFeedbackService;
/*
 * @Description  根据当前登录用户id来获取分数管理页面信息
 * @Param
 * @return
 **/
    public IPage<GradeManage> queryGradeManage(IPageGradeManage param){
        UserInfo user = sysUserBusiness.getUserInfo();
//        UserInfo user =new UserInfo();
//        user.setId(5L);
        IPage<GradeManage> gradeManage= gradeManageService.queryGradeManage(new Page<>(param.getCurrent(), param.getSize()),
                new QueryWrapper<GradeManage>().lambda()
                .eq(GradeManage::getId,user.getId())
                .like(ObjectUtil.isNotNull(param.getCourseName()),GradeManage::getCourseName,param.getCourseName())
                .eq(GradeManage::getIsDelete,0)
                .like(ObjectUtil.isNotNull(param.getEmpName()),GradeManage::getEmpName,param.getEmpName())
                .eq(ObjectUtil.isNotEmpty(user.getId()),GradeManage::getCourseBelong,user.getId())
        );
//        gradeManage.getRecords().forEach(r -> r.setIsQualified(r.getGrade() >= 60 ? "合格" : "不合格"));


        return gradeManage;
    }

        /*
         * @Description  分页查询所有的课程信息
         * @Param
         * @return
         **/
    public IPage<TrainCourse> queryAllCourse(IPageCourse param){
        UserInfo login = sysUserBusiness.getUserInfo();
        IPage<TrainCourse> courses=trainCourseService.page(new Page<>(param.getCurrent(),param.getSize()),
                new QueryWrapper<TrainCourse>().lambda()
            .eq(ObjectUtil.isNotEmpty(login.getId()),TrainCourse::getCourseBelong,login.getId())
        );
        return courses;
    }
    
    /*
     * @Description 查询没有选择该门课的教员的信息  用所有员工ID排除
     * @Param 
     * @return 
     **/
    public List<SysUser> queryUserPage(FindUserByCourse param){
        UserInfo login = sysUserBusiness.getUserInfo();
        List<SysUserRole> userRole =sysUserRoleService.list(new QueryWrapper<SysUserRole>()
        .lambda().eq(SysUserRole::getRoleId,4));
        List<SysUser> users = CollUtil.isNotEmpty(userRole)? sysUserService.list(
        new QueryWrapper<SysUser>().lambda()
        .in(SysUser::getId,userRole.stream().map(SysUserRole::getUserId).collect(Collectors.toSet()))) :null;

        if(ObjectUtil.isNotNull(param.getId())){

        List<GradeCommentFeedback> gradeComment =gradeCommentFeedbackService.list(new QueryWrapper<GradeCommentFeedback>().lambda()
        .eq(ObjectUtil.isNotEmpty(param.getId()),GradeCommentFeedback::getCourseId,param.getId())
        .eq(ObjectUtil.isNotEmpty(login.getId()),GradeCommentFeedback::getTeacherId,login.getId())
        );
        if(gradeComment.isEmpty()){
            return users;
        }
        List<SysUser> user = CollUtil.isNotEmpty(gradeComment)? sysUserService.list(
                new QueryWrapper<SysUser>().lambda()
                .in(SysUser::getId,gradeComment.stream().map(GradeCommentFeedback::getEmployeeId).collect(Collectors.toSet()))):null;
        users.removeAll(user);
        }
        return users;
    }
    /*
     * @Description 添加分数管理项
     * @Param
     * @return
     **/
    public Outcome saveGradeManage(SaveGradeManage param){
        UserInfo login = sysUserBusiness.getUserInfo();
//        UserInfo login=new UserInfo();
//            login.setId(5L);

//        TrainCourse courseId =trainCourseService.getOne(new QueryWrapper<TrainCourse>().lambda()
//        .like(ObjectUtil.isNotEmpty(param.getCourseName()),TrainCourse::getCourseName,param.getCourseName()));

        SysUser user =sysUserService.getOne(new QueryWrapper<SysUser>().lambda()
        .like(ObjectUtil.isNotEmpty(param.getUserName()),SysUser::getUserName,param.getUserName()));
        if(ObjectUtil.isNotEmpty(param.getId())&&ObjectUtil.isNotEmpty(param.getUserName())&&
                gradeCommentFeedbackService.count(new QueryWrapper<GradeCommentFeedback>().lambda()
                .eq(GradeCommentFeedback::getCourseId,param.getId())
                .eq(GradeCommentFeedback::getTeacherId,login.getId())
                .eq(GradeCommentFeedback::getEmployeeId,user.getId())) >ZERO){
            return Outcome.failure("添加失败！当前员工已登记！");
        }
        GradeCommentFeedback grademanage =new GradeCommentFeedback();
        grademanage.setCourseId(param.getId());
        grademanage.setEmployeeId(user.getId());
        grademanage.setTeacherId(login.getId());
        grademanage.setCourseGrade(param.getGrade());
        grademanage.setTeacherComment(param.getComment());
        grademanage.setCreateBy(1L);
        Boolean save =gradeCommentFeedbackService.save(grademanage);
        return Outcome.status(save);
    }

    /*
     * @Description  分数管理界面修改
     * @Param
     * @return
     **/
    public Outcome updateGradeManage(UpdataGradeManage param){
        UserInfo login = sysUserBusiness.getUserInfo();
//        UserInfo login =new UserInfo();
//        login.setId(5L);
        GradeCommentFeedback gcf = Convert.convert(GradeCommentFeedback.class,param);
        gcf.setUpdateBy(login.getId());
        Boolean update = gradeCommentFeedbackService.updateById(gcf);
        return Outcome.status(update);
    }
/*
 * @Description  删除分数管理项
 * @Param
 * @return
 **/
    @Transactional(rollbackFor = Exception.class)
    public Boolean delete(List<Long> id) {
        UserInfo userInfo = sysUserBusiness.getUserInfo();
        boolean updateBatchById = gradeCommentFeedbackService.updateBatchById(Convert.toList(GradeCommentFeedback.class,
                id.stream().map(i -> new GradeCommentFeedback().setUpdateBy(userInfo.getId()).setId(i)).collect(Collectors.toList())));
        boolean removeByIds = gradeCommentFeedbackService.removeByIds(id);
        return updateBatchById && removeByIds;
    }
//    public Boolean delClass(List<Long> id){
//        Boolean updateClass =testClassService.updateBatchById(Convert.toList(TestClass.class,id.stream()
//                .map(idz -> new TestClass().setUpdateBy(1L).setId(idz)).collect(Collectors.toList())));
//        Boolean delClass =testClassService.removeByIds(id);
//        return updateClass&&delClass;
//    }
}
